package com.project.common.fundamental.response;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class DataResponse<T> extends Response {
    private T data;

    protected DataResponse(int code, String message, T data) {
        super(code, message);
        this.data = data;
    }

    public static <R> DataResponse<R> of(R data) {
        return of(SUCCESS_CODE, "", data);
    }

    @JsonCreator
    public static <R> DataResponse<R> of(
        @JsonProperty("code") int code,
        @JsonProperty("message") String message,
        @JsonProperty("data") R data) {
        return new DataResponse<>(code, message, data);
    }

    public T getData() {
        return data;
    }

    @JsonIgnore
    public boolean isError() {
        return data != null && Throwable.class.isAssignableFrom(data.getClass());
    }
}
